﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Enyim.Caching;
using System.IO;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 使用分布式缓存系统,在使用前，请配置好相应的服务器
    /// </summary>
    #region 相应WEB.CONFIG配置如下
    /*
    <configuration>
      <configSections>
        <sectionGroup name="enyim.com">
          <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
        </sectionGroup>
        <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
      </configSections>
      <enyim.com>
        <memcached>
          <servers>
            <!-- put your own server(s) here-->
            <add address="192.168.10.29" port="11011" />
            <!--<add address="huhm2" port="20004" />-->
          </servers>
          <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
        </memcached>
      </enyim.com>
      <memcached keyTransformer="Enyim.Caching.TigerHashTransformer, Enyim.Caching">
        <servers>
          <add address="192.168.10.29" port="11011" />
          <!--<add address="huhm2" port="20004" />-->
        </servers>
        <socketPool minPoolSize="2" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
      </memcached>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
    </configuration>

     * */
    #endregion
    public class MemcachedCacheStrategy : ICacheStrategy
    {
        MemcachedClient mClient = null; 
        public MemcachedCacheStrategy()
        { 
            mClient= new MemcachedClient();
        }
        public void Add(string CacheKey, object o)
        {
            mClient.Store(StoreMode.Set, CacheKey, o);
        }

        public void Add(string CacheKey, object o, int timeoutminute)
        {
            mClient.Store(StoreMode.Set, CacheKey, o,DateTime.Now.AddMinutes(timeoutminute));
        }
        /// <summary>
        /// 使用Memcached,没有依亮键的使用方法，
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="o"></param>
        /// <param name="dependKey"></param>
        public void Add(string CacheKey, object o, string[] dependKey)
        {
            mClient.Store(StoreMode.Set, CacheKey, o);
        }

        public void Remove(string CacheKey)
        {
            mClient.Remove(CacheKey);
        }

        public object Get(string CacheKey)
        {
            return  mClient.Get(CacheKey);
        }

        // 默认缓存存活期为1440分钟(24小时)
        protected int _timeOut = 1440;
        /// <summary>
        /// 设置到期相对时间[单位:分钟] 
        /// </summary>
        public virtual int TimeOut
        {
            set { _timeOut = value > 0 ? value : 6000; }
            get { return _timeOut > 0 ? _timeOut : 6000; }
        }       

        public bool Clear()
        {
            mClient.FlushAll();
            return true;
            //throw new NotImplementedException();
        }
    }
}
